﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace TestService.Common.Config
{
    public sealed class TestServiceSection : ConfigurationSection
    {
        private static readonly ConfigurationProperty s_property = new ConfigurationProperty(string.Empty, typeof(TheKeyValueCollection), null,
                                        ConfigurationPropertyOptions.IsDefaultCollection);

        [ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
        public TheKeyValueCollection KeyValues
        {
            get
            {
                return (TheKeyValueCollection)base[s_property];
            }
        }

        public static TestServiceSection GetConfig()
        {
            TestServiceSection configSection= (TestServiceSection)ConfigurationManager.GetSection("test");
            if (configSection == null)
                throw new ConfigurationErrorsException("Section test is not found.");
            return configSection;
        }

        public static TestServiceSection GetConfig(string configPath)
        {
            var fileMap = new ExeConfigurationFileMap() { ExeConfigFilename = configPath };
            var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            TestServiceSection configSection = (TestServiceSection)config.GetSection("test");
            if (configSection == null)
                throw new ConfigurationErrorsException("Section test is not found.");
            return configSection;
        }
    }
    
    [ConfigurationCollection(typeof(TheKeyValue))]
    public class TheKeyValueCollection : ConfigurationElementCollection        // 自定义一个集合
    {
        new TheKeyValue this[string name]
        {
            get
            {
                return (TheKeyValue)base.BaseGet(name);
            }
        }

        // 下面二个方法中抽象类中必须要实现的。
        protected override ConfigurationElement CreateNewElement()
        {
            return new TheKeyValue();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((TheKeyValue)element).Name;
        }
    }

    public class TheKeyValue : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return this["name"].ToString(); }
            set { this["name"] = value; }
        }

        [ConfigurationProperty("uri", IsRequired = true)]
        public string Uri
        {
            get { return this["uri"].ToString(); }
            set { this["uri"] = value; }
        }

        [ConfigurationProperty("service", IsRequired = true)]
        public string Service
        {
            get { return this["service"].ToString(); }
            set { this["service"] = value; }
        }

        [ConfigurationProperty("contract", IsRequired = true)]
        public string Contract
        {
            get { return this["contract"].ToString(); }
            set { this["contract"] = value; }
        }
    }
}
